<template>
	<div>
		<el-dialog title="重复用药自定义" :visible.sync="dialogVisibleJiLiang" width="1025px" :before-close="handleClose">
			<el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
				<el-tab-pane label="多条件重复用药自定义设置" name="first">
					<GzCard ref="GzCard" @selectPop="selectPop" type="gz1" :gzData="gzData" @resetFun="resetFun"></GzCard>
					<KsysysCard ref="KsysysCard" @selectPop="selectPop" :mzksData="mzksData" :jzksData="jzksData" :zyksData="zyksData" :ysData="ysData"></KsysysCard>
					<BrysCard ref="BrysCard"></BrysCard>
					<ZdbrysCard ref="ZdbrysCard" @selectPop="selectPop" :zdData="zdData" :gmyData="gmyData" :ssData="ssData" :jyData="jysData" :gytjData="gytjData" @jyShow="jyShow"></ZdbrysCard>
					<cfyySetCard ref="cfyySetCard"  @selectPop="selectPop" :ypData="ypData"></cfyySetCard>
					<div style="margin-top:20px;overflow:hidden;">
						<el-button type="primary" size="medium" @click="onSubmit" style="float:right;">保存</el-button>
					</div>
					
				</el-tab-pane>
				<el-tab-pane label="多条件重复用药自定义历史列表" name="second">
					<Graytable :tHead="tHead" :tData="tData" @operateClick="operateClick"></Graytable>
					<pagination
						:total="query.total"
						:page.sync="query.page"
						:limit.sync="query.limit"
						@pagination="getList"/>
				</el-tab-pane>
			</el-tabs>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisibleJiLiang = false">关 闭</el-button>
			</span>
		</el-dialog>
		<SelectDialog ref="selectDialog" :checkId="checkId" :ctype="ctype" :multi="multi" @selectFun="selectFun"></SelectDialog>
		<LookMultiInfo ref="LookMultiInfo" ctype="重复用药" :dataObj="detailData"></LookMultiInfo>
		<jyDialog ref="jyDialog" @selectPop="selectPop" :jyData="jyData" @selectFun="jySelect"></jyDialog>
	</div>
</template>

<script>
	import SelectDialog from "./selectDialog";
	import LookMultiInfo from "./lookMultiInfo";
	import GzCard from "./gzCard";
	import KsysysCard from "./ksysysCard";
	import BrysCard from "./brysCard";
	import ZdbrysCard from "./zdbrysCard";
	import cfyySetCard from "./cfyySetCard";
	import jyDialog from "./jyDialog";
	export default {
		components: {
			SelectDialog,
			LookMultiInfo,
			GzCard,
			KsysysCard,
			BrysCard,
			ZdbrysCard,
			cfyySetCard,
			jyDialog
		},
		props:{
			dataObj:{
				required:true
			}
		},
		data () {
			return {
				detailData:{},
				activeName:"first",
				query: {
				  total: 0,
				  page: 1,
				  limit: 10
				},
				jsjbOptions:[
					{
					  value: '0',
					  label: '拦截'
					},
					{
					  value: '1',
					  label: '禁忌'
					},
					{
					  value: '2',
					  label: '不推荐'
					},
					{
					  value: '3',
					  label: '轻微'
					},
				],
				dialogVisibleJiLiang:false,//弹框开关
				tHead:[{
						label:"操作",
						operate:["删除","更新","查看多条"],
						align:"center",
						width:"160px"
					},
					{
						prop:"drugName",
						label:"药品名称"
					},
					{
						prop:"remark",
						label:"警示信息"
					},
					{
						prop:"operUser",
						label:"维护人"
					}],
				tData:[],
				ctype:"",
				checkId:"",
				gzData:{id:"",name:""},
				mzksData:{id:"",name:""},
				jzksData:{id:"",name:""},
				zyksData:{id:"",name:""},
				ysData:{id:"",name:""},
				zdData:{id:"",name:""},
				gmyData:{id:"",name:""},
				ssData:{id:"",name:""},
				jysData:{id:"",name:"",unequal:"",value:""},
				jyData:{id:"",name:""},
				ypData:{id:"",name:""},
				gytjData:{id:"",name:""},
				multi:""
			}
		},
		watch:{
			dataObj(val){
				// console.log(val);
			},
		},
		methods:{
			handleClick(tab, event){
				if(tab.name=="second"){
					this.query={
						page:1,
						limit:10,
						total:0
					};
					this.tData=[];
					this.getList();
				}
			},
			selectPop (type,checkId,multi) {
				this.ctype=type;
				this.checkId=checkId;
				this.multi=multi==1?false:true;
				this.$refs.selectDialog.show()
			},
			jyShow(){
				this.$refs.jyDialog.show();
			},
			async getList () {
				let res=await this.$custom.listDuptherapyFreedom({
					drugUniqueCode:this.dataObj.drugUniqueCode,
					page:this.query.page,
					pageSize:this.query.limit
				});
				if(res){
					for(var i=0;i<res.rows.length;i++){
						res.rows[i].flag=(res.rows[i].use_flag ==1?"<span class='green'>只能使用</span>":"<span class='red'>不能使用</span>")
					}
					this.tData=res.rows;
					this.query.total=res.total;
				}
			},
			resetFun(){
				this.tData=[];
				this.ctype="";
				this.checkId="";
				this.gzData={id:"",name:""};
				this.mzksData={id:"",name:""};
				this.jzksData={id:"",name:""};
				this.zyksData={id:"",name:""};
				this.ysData={id:"",name:""};
				this.zdData={id:"",name:""};
				this.gmyData={id:"",name:""};
				this.ssData={id:"",name:""};
				this.jysData={id:"",name:"",unequal:"",value:""};
				this.jyData={id:"",name:""};
				this.ypData={id:"",name:""};
				this.gytjData={id:"",name:""};
				this.activeName="first";
				var that=this;
				setTimeout(function(){
					that.$refs.GzCard.resetFun();
					that.$refs.KsysysCard.resetFun();
					that.$refs.BrysCard.resetFun();
					that.$refs.ZdbrysCard.resetFun();
					that.$refs.cfyySetCard.resetFun();
				},10)
			},
			show(){
				this.resetFun();
				this.dialogVisibleJiLiang = true;
			},
			async onSubmit () {
				if(!this.$refs.cfyySetCard.submit.drug_mc){
					this.$message({
					  message:"请选择要设置的药品",
					  type: 'error',
					  duration:1500
					});
					return false;
				}
				if(!this.$refs.cfyySetCard.submit.remark){
					this.$message({
					  message:"请输入要设置的警示信息",
					  type: 'error',
					  duration:1500
					});
					return false;
				}
				let newObj = {drugUniqueCode:this.dataObj.drugUniqueCode};
				Object.assign(newObj,this.$refs.GzCard.submit,this.$refs.KsysysCard.submit,this.$refs.BrysCard.submit,this.$refs.ZdbrysCard.submit,this.$refs.cfyySetCard.submit);
				let res=await this.$custom.saveDuptherapyFreedom(newObj)
				if(res){
					this.$message({
					  message: res.msg,
					  type: res.code == 1?'success':'error',
					  duration:1500
					});
					if(res.code == 1){
						this.resetFun();
					}
					this.$emit("changeFun")
				}
			},
			handleClose () {
				this.dialogVisibleJiLiang = false;
			},
			jySelect(obj){
				this.jysData=obj
			},
			selectFun(id,name){
				switch (this.ctype){
					case '规则':
						this.gzData={
							name:name,
							id:id
						}
						break;
					case '门诊科室':
						this.mzksData={
							name:name,
							id:id
						}
						break;
					case '急诊科室':
						this.jzksData={
							name:name,
							id:id
						}
						break;
					case '住院科室':
						this.zyksData={
							name:name,
							id:id
						}
						break;
					case '医生':
						this.ysData={
							name:name,
							id:id
						}
						break;
					case '诊断':
						this.zdData={
							name:name,
							id:id
						}
						break;
					case '过敏源':
						this.gmyData={
							name:name,
							id:id
						}
						break;
					case '手术':
						this.ssData={
							name:name,
							id:id
						}
						break;
					case '检验':
						this.jyData={
							name:name,
							id:id
						};
						break;
					case '给药途径':
						this.gytjData={
							name:name,
							id:id
						}
						break;
					case '药品':
						this.ypData={
							name:name,
							id:id
						}
						break;
				}
			},
			async operateClick (val,row) {
				if(val == "删除"){
					this.$confirm('确定删除通用自定义?', '提示', {
						confirmButtonText: '确定删除',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.delFun(row);
					}).catch(() => {
					});
					
				}else if(val == "更新"){
					this.activeName="first";
					this.resetFun();
				}else if(val == "查看多条"){
					let res=await this.$custom.detailDuptherapyFreedom({id:row.id})
					if(res){
						this.detailData=res;
						this.$refs.LookMultiInfo.show();
					}
				}
			},
			async delFun(row){
				let res=await this.$custom.delDuptherapyFreedom({id:row.id,drugUniqueCode:this.dataObj.drugUniqueCode});
				if(res){
					this.$message({
					  message: res.msg,
					  type: res.code == 1?'success':'error',
					  duration:1500
					});
					this.query={
						page:1,
						limit:10,
						total:0
					};
					this.getList();
					this.$emit("changeFun");
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
